<template>
  <div class="modal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">
            <slot name="title"> 修改乘客信息 </slot>
          </h4>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal"
            aria-label="Close"
          ></button>
        </div>
        <div class="modal-body">
          <form role="form" class="form-horizontal">
            <div class="row mb-2 align-items-center">
              <label class="control-label col-4 text-end"> 姓名 </label>
              <div class="col-8">
                <input
                  type="text"
                  class="form-control"
                  v-model.trim="psgInfo.name"
                />
              </div>
            </div>
            
            <div class="row mb-2 align-items-center">
              <label class="control-label col-4 text-end"> 手机 </label>
              <div class="col-8">
                <input
                  type="text"
                  class="form-control"
                  v-model.trim="psgInfo.mobile"
                />
              </div>
            </div>
            <div class="row mb-2 align-items-center">
              <label class="control-label col-4 text-end"> 证件号 </label>
              <div class="col-8">
                <input
                  type="text"
                  class="form-control"
                  v-model.trim="psgInfo.idNo"
                />
              </div>
            </div>
            <div class="row mb-2 align-items-center">
              <label class="control-label col-4 text-end"> 生日 </label>
              <div class="col-8">
                <input
                  type="text"
                  class="form-control"
                  v-model.trim="psgInfo.birthday"
                />
              </div>
            </div>
            <div class="row mb-2 align-items-center">
              <label class="control-label col-4 text-end"> 乘机人类型 </label>
              <div class="col-8">
                <select
                    class="form-select"
                    v-model.number="psgInfo.psgType"
                    placeholder=""
                  >
                    <option value="0">成人</option>
                    <option value="1">儿童</option>
                    <option value="2">婴儿</option>
                  </select>
              </div>
            </div>
            <div class="row mb-2 align-items-center">
              <label class="control-label col-4 text-end"> 性别 </label>
              <div class="col-8">
                 <select
                    class="form-select"
                    v-model.number="psgInfo.gender"
                    placeholder=""
                  >
                    <option value="-1"></option>
                    <option value="1">男</option>
                    <option value="2">女</option>
                  </select>
              </div>
            </div>
            <div class="row mb-2 align-items-center">
              <label class="control-label col-4 text-end"> 常旅客号 </label>
              <div class="col-8">
                <input
                  type="text"
                  class="form-control"
                  v-model.trim="psgInfo.ffpNo"
                />
              </div>
            </div>
            <div class="row mb-2 align-items-center">
              <label class="control-label col-4 text-end"> 备注 </label>
              <div class="col-8">
                <input
                  type="text"
                  class="form-control"
                  v-model.trim="psgInfo.remark"
                />
              </div>
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-secondary me-2"
            data-bs-dismiss="modal"
          >
            关闭
          </button>
          <button type="button" class="btn btn-primary" @click.stop="ok()">
            确定
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import $ from "jquery";

export default {
  data() {
    return {
      resolve: "",
      reject: "",
      promise: "", // 保存promise对象

      psgInfo: {
        id: "",
        name: "",
        idNo: "",
        birthday: "",
        ffpNo: "",
        mobile: "",
        remark: "",
        psgType: -1,
        gender: -1
      },

      myModal: null,
    };
  },
  methods: {
    ok: function () {
      this.hideModal();
      this.resolve(this.psgInfo);
    },
    modal: function (passenger) {
      // console.log(passenger)
      this.psgInfo.id = passenger.id;
      this.psgInfo.name = passenger.name;
      this.psgInfo.idNo = passenger.idNo;
      this.psgInfo.birthday = passenger.birthday;
      this.psgInfo.ffpNo = passenger.ffpNo;
      this.psgInfo.mobile = passenger.mobile;
      this.psgInfo.remark = passenger.remark;
      this.psgInfo.psgType = passenger.psgType;
      this.psgInfo.gender = passenger.gender;

      this.showModal();

      this.promise = new Promise((resolve, reject) => {
        this.resolve = resolve;
        this.reject = reject;
      });
      return this.promise; // 返回promise对象,给父级组件调用
    },
    showModal: function () {
      this.getMyModal().show();
    },
    hideModal: function () {
      this.getMyModal().hide();
    },
    getMyModal: function () {
      if (this.myModal === null) {
        let myModalEl = $(this.$el).get(0);
        this.myModal = new bootstrap.Modal(myModalEl, {
          keyboard: true,
        });
      }

      return this.myModal;
    },
  },
};
</script>
